//
//  PostVC.m
//  sensen_ios
//
//  Created by mc on 16/3/3.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "PostVC.h"
#import "PostListVC.h"
#import "HMSegmentedControl.h"
#import "SearchPostVC.h"

@interface PostVC ()<UIScrollViewDelegate>{
    NSArray * aryTitle;  //订单标题
    NSArray * aryStatus; //订单状态
    
}

@property (strong, nonatomic)  HMSegmentedControl *segTitle;
@property (strong, nonatomic)  UIScrollView *svContent;

@end

@implementation PostVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的帖子";
    self.view.backgroundColor = rgba(239, 239, 244, 1);
    [self createLeftBarButtonItemWithImagename:nil]; // 创建左侧返回按钮
    [self createRightBarButtonItemWithImagename:@"论坛首页_搜索"]; // 导航栏右侧按钮
    [self _initViews];
    [self _initData];
    
    
}

- (void)createRightBarButtonItemWithImagename:(NSString *)imgname {
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setImage:[UIImage imageNamed:imgname] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    // 设置背景颜色
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_BLUE_COLOR] forBarMetrics:UIBarMetricsDefault];
    
    // 隐藏标签栏
    MainTabBarController *main = (MainTabBarController *)self.navigationController.tabBarController;
    [main hideTabBar];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    // 显示标签栏
    MainTabBarController *main = (MainTabBarController *)self.navigationController.tabBarController;
    [main showTabBar];
}

#pragma mark -- 创建视图 --
- (void)_initViews{
    self.segTitle = [[HMSegmentedControl alloc] init];
    [self.view addSubview:self.segTitle];
    [self.segTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(0);
        make.height.equalTo(@(36));
    }];
    self.svContent = [[UIScrollView alloc] init];
    [self.view addSubview:self.svContent];
    [self.svContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(self.segTitle.mas_bottom).offset(0);
        make.bottom.equalTo(self.view).offset(0);
        
    }];
    
}

#pragma mark -- 初始化数据 --
- (void)_initData{
    aryTitle = @[@"我的主题",@"我的回贴",@"我的评论", @"回复我的", @"评论我的"];
    
    self.segTitle.sectionTitles = aryTitle;
    self.segTitle.backgroundColor = [UIColor whiteColor];
    self.segTitle.font = [UIFont boldSystemFontOfSize:14.0];
    self.segTitle.scrollEnabled = YES;
    self.segTitle.textColor =  [UIColor darkGrayColor];
    self.segTitle.selectedTextColor = SS_NAVI_BLUE_COLOR ;//[BoyeTool colorFromHexRGB:@"ff5657"];
    self.segTitle.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segTitle.selectionStyle =  HMSegmentedControlSelectionStyleFullWidthStripe;
    self.segTitle.selectionIndicatorHeight = 2;
    self.segTitle.segmentEdgeInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.segTitle.selectionIndicatorColor = SS_NAVI_BLUE_COLOR;
    __weak typeof(self) weakSelf = self;
    [self.segTitle setIndexChangeBlock:^(NSInteger index) {
        [weakSelf changeSelect:index animated:YES];
    }];
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, SCREEN_WIDTH, 1)];
    lbl.backgroundColor = [BoyeTool colorFromHexRGB:@"dbdbdb"];
    
    [self.view addSubview:self.segTitle];
    [self.segTitle addSubview:lbl];
    
    self.svContent.pagingEnabled = YES;
    self.svContent.showsHorizontalScrollIndicator = NO;
    self.svContent.contentSize = CGSizeMake(SCREEN_WIDTH*aryTitle.count, 200);
    self.svContent.delegate = self;
    [self performSelector:@selector(delaySelect) withObject:nil afterDelay:0.1];
}

- (void)delaySelect{
    [self changeSelect:self.intFirstSelect animated:NO];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    [self.segTitle setSelectedSegmentIndex:page animated:YES];
    [self addViewController:page];
}

- (void)changeSelect:(NSInteger)intSelect animated:(BOOL)animated{
    [self.segTitle setSelectedSegmentIndex:intSelect animated:animated];
    [self.svContent setContentOffset:CGPointMake(SCREEN_WIDTH*intSelect, 0) animated:animated];
    [self addViewController:intSelect];
}

- (void)addViewController:(NSInteger)index{
    NSMutableArray *maryIndex = [[NSMutableArray alloc]init];
    for (UIViewController *vc in self.childViewControllers) {
        if ([vc isKindOfClass:[PostListVC class]]) {
            [maryIndex addObject:[NSNumber numberWithInteger:vc.view.tag]];
        }
        
    }
    
    if (![maryIndex containsObject:[NSNumber numberWithInteger:index]]) {
        PostListVC *vc =  [[PostListVC  alloc] init];
        vc.type = index;
        [self addChildViewController:vc];
        vc.view.frame = CGRectMake(SCREEN_WIDTH * index, 0, self.svContent.frame.size.width, self.svContent.frame.size.height);
        [self.svContent addSubview:vc.view];
        vc.view.tag = index;
    }
}


// 自定义右侧按钮事件
- (void)rightAction:(UIButton *)button{
    SearchPostVC *vc = [[SearchPostVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
